<?php
class Fields extends MY_Controller{
	var $dontdisplaythesefields = array("id","createdon","updatedon","createdby","updatedby","valid" , "page" , "field" , "user_group");
			
	function __construct(){
		parent::__construct();
		$this->load->model(DEVELOPER.'/Field_model' , 'mDevField' ,TRUE);
		$this->folder = DEVELOPER;
		$this->mainmodel = $this->mDevField;
	}
	
	function index(){
		
			$tables = $this->db->list_tables();
			$fieldsFields = $this->db->field_data("fields");
			echo "<form action='".DEVELOPER_URL."/fields/update/' method='post'>";
			echo "<table width='90%'><tr>";
			echo "<td>Table</td>";
				foreach ($fieldsFields as $fieldsField)
				{
					if(in_array($fieldsField->name, $this->dontdisplaythesefields))
						continue;
					echo "<td align='center'>".(humanize($fieldsField->name))."</td>";
				}
			echo "</tr>";
			
			foreach ($tables as $table)
			{
			if($table == "fields")
				continue;
				
				echo "<tr><td colspan='3'><b>".humanize($table)." </b></td> </tr>";
				
			    $fields = $this->db->field_data($table);
			    
				foreach ($fields as $field)
				{
					echo "<tr>";
				    if(in_array($field->name, $this->dontdisplaythesefields))
					  	continue;
				    echo "<td>".humanize($field->name)."</td>";
					foreach ($fieldsFields as $fieldsField)
					{
						if(in_array($fieldsField->name, $this->dontdisplaythesefields))
							continue;
						$inputtype = "textbox";
						
						if($fieldsField->name == TYPE){
							echo "<td><select name=".$fieldsField->name.">";
								echo "<option value='textbox'>Textbox</option>";
								echo "<option value='checkbox'>Checkbox</option>";
								echo "<option value='ckeditor'>CK Editor</option>";
								echo "<option value='dropdown'>Dropdown</option>";
								echo "<option value='textarea'>Textarea</option>";
							echo "</select></td>";
						}
						
						else if($fieldsField->name == LABEL)
							echo "<td><input name='$table"."_".$field->name."_"."$fieldsField->name' type='textbox' value='".(($field->name != "")?humanize($field->name) : "")."'/></td>";
						else if($fieldsField->type == "tinyint")
							echo "<td><input name='$table"."_".$field->name."_"."$fieldsField->name' value=1 type='checkbox'/></td>";
						else
							echo "<td><input name='$table"."_".$field->name."_"."$fieldsField->name' type='textbox'/></td>";
					}
					echo "</tr>";
				}
				
			}
			
			echo "</table>";
			echo "<input type='submit' name='submit'>";
			echo "</form>";
	}
	
	function update(){
		$this->db->where('mainlist < 3');
		$this->db->delete('fields');
		
		if($this->submitted){
			
			$tables = $this->db->list_tables();
			$fieldsFields = $this->db->field_data("fields");
			
			foreach ($tables as $table)
			{
			if($table == "fields")
				continue;
				
				//$this->db->where('name is not nul');
		        //$this->db->delete('adminpage');
				//$tables = $this->db->set("name" , $table);
				//$tables = $this->db->set("custom" ,$this->input->post("custom"));
				//$tables = $this->db->insert('adminpage');
				
				
			    $fields = $this->db->field_data($table);
				foreach ($fields as $field)
				{
				    if(in_array($field->name, $this->dontdisplaythesefields))
					  	continue;
					$somethingtoinsert = false;
					foreach ($fieldsFields as $fieldsField)
					{
						if(in_array($fieldsField->name, $this->dontdisplaythesefields))
							continue;
						$inputtype = "textbox";
						if($fieldsField->type == "tinyint")
							$inputtype = "checkbox";
							echo "<br>";
							echo "name='$table"."_".$field->name."_".$fieldsField->name;
							//$isset = $this->input->post($table."_".$field->name."_".$fieldsField->name);
							$value = "";
							//if($isset != ""){
								$somethingtoinsert = true;
								$this->db->set($fieldsField->name, $this->input->post($table."_".$field->name."_".$fieldsField->name)); 
								echo "    ".$this->input->post($table."_".$field->name."_".$fieldsField->name);
							
							//}	
							//else 
							//	echo "";
					}
					if($somethingtoinsert){
						$this->db->set('page', $table); 
						$this->db->set('field', $field->name); 
						$this->db->set('user_group', 1); 
						$this->db->insert('fields');
					}
				}
				
			}
		}else 
			$this->redirecttorecordlist();
	}
	
	
	
	function addfields($table = ""){
		if($table != ""){
			$fieldsFields = $this->db->field_data("fields");
			 $fields = $this->db->field_data($table);
				foreach ($fields as $field)
				{
				    if(in_array($field->name, $this->dontdisplaythesefields))
					  	continue;
					$somethingtoinsert = false;
					foreach ($fieldsFields as $fieldsField)
					{
						if(in_array($fieldsField->name, $this->dontdisplaythesefields))
							continue;
						$inputtype = "textbox";
						if($fieldsField->type == "tinyint")
							$inputtype = "checkbox";
							echo "<br>";
							echo "name='$table"."_".$field->name."_".$fieldsField->name;
							//$isset = $this->input->post($table."_".$field->name."_".$fieldsField->name);
							$value = "";
							//if($isset != ""){
								$somethingtoinsert = true;
								$this->db->set($fieldsField->name, $this->input->post($table."_".$field->name."_".$fieldsField->name)); 
								echo "    ".$this->input->post($table."_".$field->name."_".$fieldsField->name);
							
							//}	
							//else 
							//	echo "";
					}
					if($somethingtoinsert){
						$this->db->set('page', $table); 
						$this->db->set('field', $field->name); 
						$this->db->set('user_group', 1); 
						$this->db->insert('fields');
					}
				}
		}
	}
	
}